package com.wzh.mysending.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.widget.ImageView;

import com.amap.api.maps.AMapUtils;
import com.amap.api.maps.model.LatLng;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.squareup.picasso.Picasso;
import com.wzh.mysending.R;
import com.wzh.mysending.model.UserEntity;
import com.wzutils.BigDisMailUtils;


import java.util.ArrayList;
import java.util.List;

/**
 * Created by Hu on 2017/3/15.
 */

public class UserAdapter extends BaseQuickAdapter<UserEntity, BaseViewHolder> {
    double lat;
    double lot;
    Context context;

    public UserAdapter(int layoutResId, List<UserEntity> data, Context context) {
        super(layoutResId, data);
        this.context = context;
    }


    @Override
    protected void convert(BaseViewHolder helper, UserEntity item) {
        double mi = Double.parseDouble(String.valueOf(AMapUtils.calculateLineDistance(new LatLng(lat, lot),
                new LatLng(item.getLocationEntity().getLat(), item.getLocationEntity().getLot()))));
        helper
                .setText(R.id.name, item.getUserName())
                .setText(R.id.tel_no, item.getUserPhone())
                .setText(R.id.address, TextUtils.isEmpty(item.getLocationEntity().getAddRess()) ?
                        "点击这一栏可以查询地址信息。" : item.getLocationEntity().getAddRess())
                .setText(R.id.time, item.getUpdatedAt())
                .setVisible(R.id.juli,lot==0&&lat==0?false:true)
                .setText(R.id.juli, mi > 500 ? BigDisMailUtils.turn(mi / 1000, 2) + "千米" : BigDisMailUtils.turn(mi, 2) + "米");
        setImage(item.getUser_img(), (ImageView) helper.getView(R.id.iv_headportrait));
    }

    public void setLatAndLot(double lat, double lot) {
        this.lat = lat;
        this.lot = lot;
    }

    public void setImage(String url, ImageView imageView) {
        Picasso.with(context)
                .load(url)
                .placeholder(R.mipmap.logo)
                .error(R.mipmap.logo)
                .into(imageView);
    }
}
